package com.itheima.job4;

import org.junit.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Demo {
    @Test
    public void test() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Class<Person> clz = Person.class;
        Constructor<Person> constructor = clz.getConstructor(String.class, int.class);
        Person person = constructor.newInstance("张三", 23);
        System.out.println(person);
        Method setName = clz.getMethod("setName", String.class);
        setName.invoke(person, "李四");
        Field[] declaredFields = clz.getDeclaredFields();
        for (Field f : declaredFields) {
            f.setAccessible(true);
            if ("age".equals(f.getName())){
                f.set(person,25);
            }
        }
        System.out.println(person);

    }
}

